/*
工厂方法
一个工厂方法生产一个子类
*/
package factory_method

type BaseOperator struct {
	a, b int
}

type Operator interface {
	SetA(a int)
	SetB(b int)
	Result() int
}

type OperatorFactory interface {
	Create() Operator
}

type PlusOperatorFactory struct {
}
type MinusOperatorFactory struct {
}

func (p *PlusOperatorFactory) Create() Operator {
	return &plusOperator{BaseOperator: &BaseOperator{}}
}
func (p *MinusOperatorFactory) Create() Operator {
	return &minusOperator{BaseOperator: &BaseOperator{}}
}

var _ OperatorFactory = (*PlusOperatorFactory)(nil)
var _ OperatorFactory = (*MinusOperatorFactory)(nil)
var _ Operator = (*plusOperator)(nil)
var _ Operator = (*minusOperator)(nil)

// 加法
type plusOperator struct {
	*BaseOperator
}

// 减法
type minusOperator struct {
	*BaseOperator
}

func (p *plusOperator) SetA(a int) {
	p.a = a
}

func (p *plusOperator) SetB(b int) {
	p.b = b
}

func (p *plusOperator) Result() int {
	return p.a + p.b
}

func (m minusOperator) SetA(a int) {
	m.a = a
}

func (m minusOperator) SetB(b int) {
	m.b = b
}

func (m minusOperator) Result() int {
	return m.a - m.b
}
